package com.doit.demo.day02.sources;

import org.apache.flink.api.java.io.TextInputFormat;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.FileProcessingMode;

/**
 * @DATE 2022/2/15/22:12
 * @Author MDK
 * @Version 2021.2.2
 *
 *
 *   readFile可以指定多种模式
 *   默认是FileProcessingMode.PROCESS_ONCE,即数据读完就退出(有限的数据流)
 **/
public class FileSources2 {
    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",8081);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        String filePath = "C:\\Users\\MDK\\Desktop\\a.txt";
        Path path = new Path(filePath);
        TextInputFormat textInputFormat = new TextInputFormat(path);

//        DataStreamSource<String> lines = env.readFile(textInputFormat, filePath);
        //选择模式,连续读取则不退出程序,数据更新后继续从头读取;只读一次模式下,文件数据读取完成后退出程序
        DataStreamSource<String> lines = env.readFile(textInputFormat, filePath, FileProcessingMode.PROCESS_ONCE, 1000);
        //readTextFile()方法创建DataStream的并行度
        System.out.println("readTextFile方法创建的DataStream的并行度:"+lines.getParallelism());
        lines.print();
        env.execute();
    }
}
